package com.quan.system.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.core.MyBaseServiceImpl;
import com.quan.system.entity.SystemArea;
import com.quan.system.mapper.SystemAreaMapper;
import com.quan.system.service.SystemAreaService;

/**
 * <p>
 * 地区表 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-03
 */
@Service
public class SystemAreaServiceImpl extends MyBaseServiceImpl<SystemAreaMapper, SystemArea> implements SystemAreaService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Async
	@Override
	public boolean asyncUpdateLocation(SystemArea entity) {
		return super.updateById(entity);
	}
	
	@Override
	public boolean removeById(Serializable id) {
		return this.recursiveDelete(id);
	}
	
	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		for (Serializable id : idList) {
			this.recursiveDelete(id);
		}
		return true;
	}
	
	private boolean recursiveDelete(Serializable id) {
		QueryWrapper<SystemArea> queryWrapper = new QueryWrapper<SystemArea>();
		queryWrapper.eq("pid", id);
		
		List<SystemArea> child = super.list(queryWrapper) ;
		if(null != child) {
			for (SystemArea entity : child) {
				recursiveDelete(entity.getId());
			}
		}
		return super.removeById(id);
	}

	@Async
	@Override
	public void asyncGeneratorVantAreaJson() {
		
		Map<String, Object> province = new HashMap<String, Object>() ;
		Map<String, Object> city = new HashMap<String, Object>() ;
		Map<String, Object> county = new HashMap<String, Object>() ;
		
		
		QueryWrapper<SystemArea> queryWrapper1 = new QueryWrapper<SystemArea>();
		queryWrapper1.eq("level", 1);
		List<SystemArea> provinces = super.list(queryWrapper1) ;
		for (SystemArea area : provinces) {
			province.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		QueryWrapper<SystemArea> queryWrapper2 = new QueryWrapper<SystemArea>();
		queryWrapper2.eq("level", 2);
		List<SystemArea> citys = super.list(queryWrapper2) ;
		for (SystemArea area : citys) {
			city.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		QueryWrapper<SystemArea> queryWrapper3 = new QueryWrapper<SystemArea>();
		queryWrapper3.eq("level", 2);
		List<SystemArea> countys = super.list(queryWrapper3) ;
		for (SystemArea area : countys) {
			county.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		Map<String, Object> exportdefault = new HashMap<String, Object>() ;
		exportdefault.put("province_list", province);
		exportdefault.put("city_list", city);
		exportdefault.put("county_list", county);
		
		try {
			String path = "d:/" ;
			InputStream inputStream = IOUtils.toInputStream(JSON.toJSONString(exportdefault), "UTF-8") ;
			IOUtils.copy(inputStream, new FileOutputStream(path + "Area.json"));
			logger.debug("输出路径：" + path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public List<SystemArea> listWithTree() {
		// 1、查出所有分类数据
		List<SystemArea> entities = super.list();
		
		// 2、组装成父子的树形结构
		List<SystemArea> level1Category = entities.stream().filter((entity) -> {
			// 1、找到所有一级分类
			return Integer.parseInt(entity.getPid()+"") == 0;
		}).map((menu) -> {
			// 2、找到所有子菜单
			return menu.setChildren(getChildren(menu, entities));
		}).collect(Collectors.toList());
		
		return level1Category;
	}
	
	/**
	 * 递归查找所有菜单的子菜单
	 * @param root
	 * @param all
	 * @return
	 */
	public List<SystemArea> getChildren(SystemArea root, List<SystemArea> all) {
		root.setLabel(root.getName());
		List<SystemArea> children = all.stream().filter((entity) -> {
			return Integer.parseInt(entity.getPid()+"") == Integer.parseInt(root.getId()+"");
		}).map((entity) -> {
			// 1、递归找到子菜单
			entity.setLabel(entity.getName());
			return entity.setChildren(getChildren(entity, all));
		}).collect(Collectors.toList());
		
		return children;
	}
	

}
